package com.mlink.web;

import com.mlink.entity.ProjectEntity;
import com.mlink.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProjectController {
	
	@Autowired
	private ProjectMapper projectMapper;


    /*
    * 获取所有项目
    *
    * */
	@RequestMapping("/getProjects")

    public List<ProjectEntity> getProjects() {

		List<ProjectEntity> projects=projectMapper.getAll();
		return projects;
	}

    @RequestMapping("/getOneProject")
    public ProjectEntity getOneProject(int id) {

        ProjectEntity project=projectMapper.getOne(id);
        return project;
    }


    /*
    * 添加项目
    *
    * */
    @RequestMapping(value = "/addProject", method = RequestMethod.POST)
    public ModelMap save(@RequestBody ProjectEntity project) {

        ModelMap mm = new ModelMap();


        List<ProjectEntity>  pjs = projectMapper.isNameEixt(project.getName());
        if(pjs.size() >0) {
            mm.put("result", 2); // 已经存在
        } else {

            int result = projectMapper.insert(project);
            if(result == 0) {
                mm.put("result", 0); // 失败
            } else {
                mm.put("result", 1); // 成功
                mm.put("id",project.getId());
            }

        }
      return  mm;
    }

    /*
    *更新用户
    *
    * */
    @RequestMapping(value="/updateProject",method = RequestMethod.POST)
    public String update(@RequestBody ProjectEntity project) {
    	int result =  projectMapper.update(project);
        if (result == 1) {
            return "{\"result\":1}";
        } else {
            return "{\"result\":0}";
        }
    }
    
    @RequestMapping(value="/deleteProject" )
    public String delete(int id) {

        int result = projectMapper.delete(id);
        if (result == 1) {
            return "{\"result\":1}";
        } else {
            return "{\"result\":0}";
        }
    }

    @RequestMapping(value="/setProjecState" )
    public void setProjecState(int id, int state) {

        projectMapper.setState(state,id);
    }
}